* -------------------------------------------------------------------------------
*			PROGRAM BOOTRONE
*
*	residual-based bootstrap. Last updated: 10/9/23
*	program does two things:
*		--if yvalue and sigma2value are specified, it bootstraps a value of both at the same point in time, and saves them as scalars
*		--if resid is specified, it bootstraps a value of epsilon
*	same as bootr2, which preserves any missings in the original series that gets bootstrapped. But this process is to select just a single value for starting values. It always puts it at the first observed period
* -------------------------------------------------------------------------------

cap prog drop bootrone
prog define bootrone, rclass
syntax [if] [in], [yvalue(string) sigma2value(string) resid(string)]
* yvalue: DV variable to bootstrap.
* sigma2value: sigma2 variable to bootstrap.
* resid: residual variable to bootstrap
* preliminaries
qui tsset // ensure we're tsset and sorted

qui if "`yvalue'" != "" {
	preserve
		keep `yvalue' `sigma2value'
		drop if `yvalue' == . | `sigma2value' == .
		bsample 1
		su `yvalue', meanonly
		return scalar yinit = r(mean)
		su `sigma2value', meanonly
		return scalar sigma2init = r(mean)
	restore
}
else {
	preserve
		keep `resid' 
		drop if `resid' == .
		bsample 1
		su `resid', meanonly
		return scalar residvalue = r(mean)
	restore
}

end
